{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pymysql   # MySQLdb  不支持python3 用 pymysql\n",
    "server='rm-bp1witqzl76lpag95.mysql.rds.aliyuncs.com'\n",
    "outserver=\"rm-bp1witqzl76lpag957o.mysql.rds.aliyuncs.com\"\n",
    "\n",
    "xuyi_aliyun = \"121.40.53.82\"\n",
    "xy_user = \"root\"\n",
    "xy_pwd = \"Fangzhouqwer1069.\"\n",
    "\n",
    "user=\"dbuser\"\n",
    "pwd='dbuser@121'\n",
    "dbname=\"test\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    " def execute(sql):\n",
    "    # 打开数据库连接\n",
    "    db = pymysql.connect(host=server,user=user,password=pwd,database=dbname)\n",
    "\n",
    "    # 使用 cursor() 方法创建一个游标对象 cursor\n",
    "    cursor = db.cursor()\n",
    "    \n",
    "    # 使用 execute()  方法执行 SQL 查询 \n",
    "    cursor.execute(sql)\n",
    "\n",
    "    # 使用 fetchone() 方法获取单条数据.\n",
    "    data = cursor.fetchone()\n",
    "\n",
    "    print (\"Database version : %s \" % data)\n",
    "\n",
    "    # 关闭数据库连接\n",
    "    db.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# SQL 插入语句\n",
    "sql = \"\"\"INSERT INTO EMPLOYEE(FIRST_NAME,\n",
    "         LAST_NAME, AGE, SEX, INCOME)\n",
    "         VALUES ('Mac', 'Mohan', 20, 'M', 2000)\"\"\"\n",
    "\n",
    "db = pymysql.connect(host=server,user=user,password=pwd,database=dbname)\n",
    "\n",
    "# 使用 cursor() 方法创建一个游标对象 cursor\n",
    "cursor = db.cursor()\n",
    "\n",
    "try:\n",
    "   # 执行sql语句\n",
    "   cursor.execute(sql)\n",
    "   # 提交到数据库执行\n",
    "   db.commit()\n",
    "except Exception as ex:\n",
    "   # 如果发生错误则回滚\n",
    "   db.rollback()\n",
    " \n",
    "# 关闭数据库连接\n",
    "db.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "sql = \"SELECT VERSION()\"\n",
    " \n",
    "# 使用预处理语句创建表\n",
    "sql = \"\"\"CREATE TABLE EMPLOYEE (\n",
    "         FIRST_NAME  CHAR(20) NOT NULL,\n",
    "         LAST_NAME  CHAR(20),\n",
    "         AGE INT,  \n",
    "         SEX CHAR(1),\n",
    "         INCOME FLOAT )\"\"\"\n",
    "\n",
    "sql = \"INSERT INTO EMPLOYEE(FIRST_NAME, \\\n",
    "       LAST_NAME, AGE, SEX, INCOME) \\\n",
    "       VALUES ('%s', '%s',  %s,  '%s',  %s)\" % \\\n",
    "       ('Mac', 'Mohan', 20, 'M', 2000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Database version : None \n"
     ]
    }
   ],
   "source": [
    "#pymysql.connect?\n",
    "execute(sql)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
